package com.nui.multiphotopicker;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import xf.tools.Other;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.vgo.crop.view.ClipZoomImageView;
import com.vgo.crop.view.Crop_Activity;

public class Use_photos_Activity extends BaseActivity implements OnClickListener {

	private ClipZoomImageView imageView1;
	private TextView use_photos;
	private TextView back;
	private String imageUrl;

	@Override
	protected void onResume() {
		BaseActivity.activity = this;
		super.onResume();
	}

	// private ClipZoomImageView mZoomImageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.use_photos_activity);
		BaseActivity.activity = this;

		imageView1 = (ClipZoomImageView) findViewById(R.id.imageView1);
		use_photos = (TextView) findViewById(R.id.use_photos);
		back = (TextView) findViewById(R.id.back);

		imageUrl = getIntent().getStringExtra("name");
		// 预览图片
		Bitmap bitmap = getSmallBitmap(imageUrl);
		imageView1.setImageBitmap(bitmap);

		use_photos.setOnClickListener(this);
		back.setOnClickListener(this);

	}

	public static Bitmap getSmallBitmap(String filePath) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, options);
		options.inSampleSize = calculateInSampleSize(options, 480, 800);
		options.inJustDecodeBounds = false;
		Bitmap bm = BitmapFactory.decodeFile(filePath, options);
		if (bm == null) {
			return null;
		}
		int degree = readPictureDegree(filePath);
		bm = rotateBitmap(bm, degree);
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream();
			bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
		} finally {
			try {
				if (baos != null)
					baos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bm;
	}

	private static int readPictureDegree(String path) {
		int degree = 0;
		try {
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				degree = 90;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				degree = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				degree = 270;
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return degree;
	}

	private static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
		if (bitmap == null)
			return null;

		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		// Setting post rotate to 90
		Matrix mtx = new Matrix();
		mtx.postRotate(rotate);
		return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
	}

	// 计算图片的缩放值
	private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {

			final int heightRatio = Math.round((float) height / (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);

			inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
		}

		return inSampleSize;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			try {
				File file = new File(imageUrl);
				if (file.exists()) {
					file.delete();
				}
				finish();
			} catch (Exception e) {
			}

			break;
		case R.id.use_photos:
			Bundle bundle = new Bundle();
			bundle.putInt("num", 1);
			bundle.putString("imageUrl", imageUrl);
			EmmApplication.setData("Photograph_uri", imageUrl);
			Intent intent = new Intent(Use_photos_Activity.this, Crop_Activity.class);
			startActivity(intent);
			Other.Photograph_uri = EmmApplication.getData("Photograph_uri");
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
